package tk.mybatis.springboot.spider.processor;

import org.apache.http.util.TextUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import tk.mybatis.springboot.spider.entity.LiteratureEntity;
import tk.mybatis.springboot.spider.utils.UrlConfig;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

public class LiteraturePageProcessor implements PageProcessor {


    public static int num = 0;
    private Site site = Site.me()
            .setRetryTimes(20)
            .setSleepTime(15000)
            .setTimeOut(400000)
            .setCycleRetryTimes(10)
            .setRetrySleepTime(5000)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");

    @Override
    public void process(Page page) {
        /**
         * 杂志
         * http://www.sciencedirect.com/science/journal/00909092
         */
        if (page.getUrl().regex(UrlConfig.JOURNAL).match()) {
            try {
                String articleList = page.getHtml().xpath("//*[@id=\"bodyMainResults\"]/ol").get();
                Element articleOl = Jsoup.parse(articleList);
                List<Element> titles = articleOl.getElementsByClass("title");
                for (Element title : titles) {
                    String link = title.getElementsByTag("a").attr("href").toString();
                    System.out.println("******article link******" + link);
                    page.addTargetRequest(link);
                }
            } catch (Exception e) {
                e.printStackTrace();
                saveErrorLink(page, page.getUrl().toString(), e);
            }
        }
        /**
         *
         * http://www.sciencedirect.com/science/article/pii/S1062145805011177
         */
        if (page.getUrl().regex(UrlConfig.ARTICLE).match()) {
            try {
                Thread.sleep(3000);
                num++;
                System.out.println("****** 第" + num + " 条信息 ******");
                String publishName = "";
                String vol_iss = "";
                String title = "";
                String author = "";
                String doiLink = "";
                String digest = "";
                LiteratureEntity literatureEntity = new LiteratureEntity();
                String inner = page.getHtml().xpath("//*[@id=\"centerInner\"]").get();
                String publication = page.getHtml().$(".Publication").get();
                if (!TextUtils.isBlank(inner)) { // 付费文档
                    Element innerDiv = Jsoup.parse(inner);
                    Element head = innerDiv.getElementsByClass("head").first();
                    publishName = head.getElementsByClass("title").text();
                    System.out.println("******publishName******" + publishName);
                    vol_iss = head.getElementsByClass("volIssue").text();
                    System.out.println("******volume_issue******" + vol_iss);
//            Volume 14, Issue 12, December 2005, Pages 4–5
                    subInfo(vol_iss, literatureEntity);
                    Element titleDiv = innerDiv.getElementsByClass("page_fragment").first();
                    Element authorUl = null;
                    if (null != titleDiv) {
                        title = titleDiv.getElementsByClass("svTitle").first().text();
                        System.out.println("******title******" + title);
                        authorUl = titleDiv.getElementsByClass("authorGroup").first();
                    }
                    if (null != authorUl) {
                        author = authorUl.text();
                        System.out.println("******author******" + author);
                    }

                    Element absE = innerDiv.getElementsByClass("abstract").first();
                    if (null != absE) {
                        digest = absE.text();
                        System.out.println("******abstract******" + digest);
                    }


                } else if (!TextUtils.isBlank(publication)) { //open access 免费文档
                    String doiLinkString = page.getHtml().$(".DoiLink").get();
                    String authorString = page.getHtml().$(".Banner").get();
                    String head = page.getHtml().$(".Head").get();
                    String abs = page.getHtml().$(".Abstracts").get();

                    Element publicationDiv = Jsoup.parse(publication);
                    Element volumeDiv = publicationDiv.getElementsByClass("publication-volume").first();
                    publishName = volumeDiv.getElementsByClass("publication-title").first().text();
                    System.out.println("######publicationName######" + publishName);
                    vol_iss = volumeDiv.getElementsByClass("size-m").first().text();
                    System.out.println("######volume_issue######" + vol_iss);
//                Volume 9 , 2014 , Pages 12-18
                    subInfo(vol_iss, literatureEntity);
                    if (!TextUtils.isBlank(head)) {

                        Element titleHeadE = Jsoup.parse(head);
                        title = titleHeadE.text();
                        System.out.println("######title######" + title);
                    }
                    if (!TextUtils.isBlank(authorString)) {

                        Element authorE = Jsoup.parse(authorString);
                        List<Element> list = authorE.getElementsByClass("Author");
                        StringBuffer authorBuffer = new StringBuffer();
                        for (int i = 0; i < list.size(); i++) {
                            Element authorSpan = list.get(i).getElementsByClass("author-name").first();
                            authorBuffer.append(authorSpan.text());
                            if (i != list.size() - 1) {
                                authorBuffer.append(",");
                            }
                        }
                        author = authorBuffer.toString();
                        System.out.println("######author######" + author);
                    }

                    if (!TextUtils.isBlank(doiLinkString)) {
                        Element doiE = Jsoup.parse(doiLinkString);
                        doiLink = doiE.getElementsByClass("doi").first().text();
                        System.out.println("######doiLink######" + doiLink);

                    }

                    if (!TextUtils.isBlank(abs)) {
                        Element absE = Jsoup.parse(abs);
                        digest = absE.text();
                        System.out.println("######abstract######" + digest);
                    }


                }

                literatureEntity.setPublicationName(publishName);
                literatureEntity.setTitle(title);
                literatureEntity.setAuthor(author);
                literatureEntity.setDigest(digest);
                literatureEntity.setDoiLink(doiLink);
                literatureEntity.setDelFlag(0);
                literatureEntity.setSiteUrl("sciencedirect");
                page.putField("literature", literatureEntity);

            } catch (Exception e) {
                e.printStackTrace();
                saveErrorLink(page, page.getUrl().toString(), e);
            }
        }

    }


    @Override
    public Site getSite() {
        return this.site;
    }

    /**
     * 获取必要字段
     *
     * @param vol_iss
     */
    public void subInfo(String vol_iss, LiteratureEntity literatureEntity) {
        String[] infos = vol_iss.split(",");
        for (int i = 0; i < infos.length; i++) {

            if (infos[i].contains("Volume")) {
                String volume = infos[i].substring(infos[i].indexOf("Volume") + 6);
                literatureEntity.setVolume(volume);
                System.out.println("******volume******" + literatureEntity.getVolume());

            } else if (infos[i].contains("Issue")) {
                String issue = infos[i].substring(infos[i].indexOf("Issue") + 5);
                literatureEntity.setIssue(issue);
                System.out.println("******issue******" + literatureEntity.getIssue());

            } else if (infos[i].contains("Pages")) {
                String pages = infos[i].substring(infos[i].indexOf("Pages") + 5);
                String startPage = "";
                String endPage = "";
//                String c = URLDecoder.decode("%E2%80%93");
                if (pages.contains("–")) {
                    startPage = pages.substring(0, pages.indexOf("–"));
                    System.out.println("******startPage******" + startPage);
                    endPage = pages.substring(pages.indexOf("–") + 1);
                    System.out.println("******endPage******" + endPage);
                } else if (pages.contains("-")) {
                    startPage = pages.substring(0, pages.indexOf("-"));
                    System.out.println("******startPage******" + startPage);
                    endPage = pages.substring(pages.indexOf("-") + 1);
                    System.out.println("******endPage******" + endPage);
                } else {
                    startPage = pages;
                    System.out.println("******startPage******" + startPage);
                }
                literatureEntity.setStartPage(startPage);
                literatureEntity.setEndPage(endPage);

            } else {
                String publishTime = infos[i].toString();
                literatureEntity.setPubtime(publishTime);
                System.out.println("******publishTime******" + literatureEntity.getPubtime());

            }
        }
    }


    /**
     * 添加爬虫请求
     * 保存请求失败页面
     *
     * @param page
     * @param link
     */

    public void saveErrorLink(Page page, String link, Exception e) {
        LiteratureEntity literatureEntity = new LiteratureEntity();
        literatureEntity.setCompany(link);
        literatureEntity.setTitle("errorLink");
        literatureEntity.setSiteUrl("informa");
        if (null != e) {
            literatureEntity.setAuthor(e.getMessage());
        }
        page.putField("literature", literatureEntity);
    }

//    public static void main(String[] args) {
//
//
//    }


}